
clear
do "...\First.do"
********************************************************************************
use "$work\analysis_sample.dta"
keep if inrange(age,40,70)

g help=1 if inrange(timing,-4,0)
bys pnr: egen N_pre_2=total(help)
tab N_pre_2
keep if N_pre_2==5

drop if timing>3
drop if timing<-3

********************************************************************************
*** DDD
********************************************************************************
cap drop interaction2 interaction3 ddd3
g interaction2=low_ses*help3
g interaction3=low_ses*post
g ddd3=low_ses*help3*post

label var help3 "PCP low SES"
label var post "Post"
label var low_ses "Patient low SES"
label var interaction "PCP low SES x Post"
label var interaction2 "PCP low SES x Patient low SES"
label var interaction3 "Patient low SES x Post"
label var ddd3 "PCP low SES x Patient low SES x Post"


*** DETECTION VS. ADHERENCE
cap drop pre_statins
cap drop pre_diabetes
g pre_statins=(statins==1 & post==0)
g pre_metformin=(metformin==1 & post==0)
g pre_copd=((ACSC_COPD==1 | COPD_med==1) & post==0)
g pre_ace=(ACE==1 & post==1)

foreach var of varlist pre_statins pre_metformin pre_copd pre_ace {
	
 bys pnr: egen d_`var'=max(`var')
	drop `var'
	
}

g d_pre_CVC=(d_pre_statins==1 | d_pre_ace==1)

cap drop any
egen any=rowmax(d_pre_statins d_pre_ace d_pre_metformin d_pre_copd)
egen N_conditions=rowtotal(d_pre_CVC d_pre_metformin d_pre_copd)
g condition_1=(N_conditions==1)
g condition_2=(N_conditions==2)
g condition_3=(N_conditions==3)




********************************************************************************
* Estimated together
********************************************************************************

* No conditions
sum death if any==0 & post==1, d
local mean=round(`r(mean)'*1000/1000, 0.00001)
local sd=round(`r(sd)'*1000/1000, 0.00001)


sum death if post==1 & low_ses==1 & any==0
local mean_low=round(`r(mean)'*1000/1000, 0.00001) 

ttest death if post==1 & help3==0 & any==0 & post==1 , by(low_ses)
local mean0= round((`r(mu_2)'-`r(mu_1)')*1000/1000, 0.00001)


reghdfe death help3 low_ses interaction2 $patient_X $gp_X if any==0 & post==1, a(gp_fe year) cluster(newgpyr)


outreg2 using "$table\TableC1.doc", replace tex label keep(help3 low_ses interaction2) nocons bdec(5) sdec(5) rdec(5) addtext(Outcome mean,`mean', Outcome mean low-SES, `mean_low', Gradient  for high SES physicians, `mean0') ctitle(No conditions)



* Any condition
sum death if any==1 & post==1, d
local mean=round(`r(mean)'*1000/1000, 0.00001)
local sd=round(`r(sd)'*1000/1000, 0.00001)


sum death if post==1 & low_ses==1 & any==1
local mean_low=round(`r(mean)'*1000/1000, 0.00001) 

ttest death if post==1 & help3==0 & any==1, by(low_ses)
local mean0= round((`r(mu_2)'-`r(mu_1)')*1000/1000, 0.00001)


reghdfe death help3 low_ses interaction2 $patient_X $gp_X if post==1 & any==1, a(gp_fe year) cluster(newgpyr)


outreg2 using "$table\TableC1.doc", append tex label keep(help3 low_ses interaction2) nocons bdec(5) sdec(5) rdec(5) addtext(Outcome mean,`mean', Outcome mean low-SES, `mean_low', Gradient  for high SES physicians, `mean0') ctitle(Any conditions)



* 1 condition
sum death if condition_1==1 & low_ses==0 & post==1, d
local mean=round(`r(mean)'*1000/1000, 0.00001)
local sd=round(`r(sd)'*1000/1000, 0.00001)


sum death if post==1 & low_ses==1 & condition_1==1
local mean_low=round(`r(mean)'*1000/1000, 0.00001) 

ttest death if post==1 & help3==0 & condition_1==1, by(low_ses)
local mean0= round((`r(mu_2)'-`r(mu_1)')*1000/1000, 0.00001)


reghdfe death help3 low_ses interaction2 $patient_X $gp_X if post==1 & condition_1==1, a(gp_fe year) cluster(newgpyr)


outreg2 using "$table\TableC1.doc", append tex label keep(help3 low_ses interaction2) nocons bdec(5) sdec(5) rdec(5) addtext(Outcome mean,`mean', Outcome mean low-SES, `mean_low', Gradient  for high SES physicians, `mean0') ctitle(1 condition)


* 2 condition
sum death if condition_2==1  & post==1, d
local mean=round(`r(mean)'*1000/1000, 0.00001)
local sd=round(`r(sd)'*1000/1000, 0.00001)


sum death if post==1 & low_ses==1 & condition_2==1
local mean_low=round(`r(mean)'*1000/1000, 0.00001) 

ttest death if post==1 & help3==0 & condition_2==1, by(low_ses)
local mean0= round((`r(mu_2)'-`r(mu_1)')*1000/1000, 0.00001)


reghdfe death help3 low_ses interaction2 $patient_X $gp_X if post==1 & condition_2==1, a(gp_fe year) cluster(newgpyr)


outreg2 using "$table\TableC1.doc", append tex label keep(help3 low_ses interaction2) nocons bdec(5) sdec(5) rdec(5) addtext(Outcome mean,`mean', Outcome mean low-SES, `mean_low', Gradient  for high SES physicians, `mean0') ctitle(2 conditions)




* 3 condition
sum death if condition_3==1  & post==1, d
local mean=round(`r(mean)'*1000/1000, 0.00001)
local sd=round(`r(sd)'*1000/1000, 0.00001)

sum death if post==1 & low_ses==1 & condition_3==1
local mean_low=round(`r(mean)'*1000/1000, 0.00001) 

ttest death if post==1 & help3==0 & condition_3==1, by(low_ses)
local mean0= round((`r(mu_2)'-`r(mu_1)')*1000/1000, 0.00001)

reghdfe death help3 low_ses interaction2 $patient_X $gp_X if post==1 & condition_3==1, a(gp_fe year) cluster(newgpyr)

local beta=_b[interaction2]
local effect=round(`beta'/`mean0'*100, 0.1)

outreg2 using "$table\TableC1.doc", append tex label keep(help3 low_ses interaction2) nocons bdec(5) sdec(5) rdec(5) addtext(Outcome mean,`mean', Outcome mean low-SES, `mean_low', Gradient  for high SES physicians, `mean0') ctitle(3 condition)


